/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.upload;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import queenb.web.bean.FileDescription;
import queenb.web.controller.QueenbController;
import queenb.web.crm.store.K100Store;
import queenb.web.store.EmployeeStore;
import queenb.web.util.ContextUtil;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class UploadLogoController extends QueenbController {

    private static final int IMG_WIDTH = 50;
    private static final int IMG_HEIGHT = 50;
    private int pk100;
    private int pn100;
    private String type = "jpg,png,bmp,gif";
    private String reload;
    private String pagereload = "";
    private int beginContructor;

    public UploadLogoController() {
        try {
            Object pk100Object = ContextUtil.getRequestParameter("pk100");
            if (pk100Object != null) {
                this.pk100 = Util.parseInt(pk100Object);
            }
        } catch (Exception e) {
        }
        try {
            Object pn100Object = ContextUtil.getRequestParameter("pn100");
            if (pn100Object != null) {
                this.pn100 = Util.parseInt(pn100Object);
            }
        } catch (Exception e) {
        }

        Object pvtype = getRequest().getParameter("type");
        if (pvtype != null) {
            type = "*";
        }


        Object pagere = ContextUtil.getRequestParameter("page");
        if (pagere != null) {
            this.pagereload = pagere.toString();
        }
        if (this.pagereload == null) {
            this.pagereload = ContextUtil.getRequestParameter("page");
        }

        //Upload image for webcame
        Object objAction = getRequest().getParameter("action");
        if (objAction != null) {
            if (objAction.equals("webcame")) {
                String typep = getRequest().getParameter("type");
                String image = getRequest().getParameter("image");


                System.out.println("SAVING>>>>>>>>>>>>>>>>>>>>Type=" + pn100);

                if (typep != null) {
                    EmployeeStore storeN100 = new EmployeeStore();
                    //   storeN100.updateColumnLogoN100(2888, imageByte, getLogin().getUserName());
                    storeN100.updateColumnLongText(null, "N100", "NV126", image, "PN100", pn100, getLogin().getUserName());
                    System.out.println("----imag string ---" + image);
                }
            }
        }
    }

    public void uploadPicListener(UploadEvent ue) throws IOException {

        System.out.println("==++++++++++++++++++++++ bat dau upload logo");
        uploadAttachment(ue, FileDescription.PIC_KIND_OF_FILE);
    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
        g.dispose();

        return resizedImage;
    }

    private void uploadAttachment(UploadEvent ue, int typeUpload) {

        List<UploadItem> uploadItem = ue.getUploadItems();

        for (int i = 0; i < uploadItem.size(); i++) {
            try {

                UploadItem uploadItem1 = uploadItem.get(i);

                File file = uploadItem1.getFile();
                System.out.println("-------" + file);
                BufferedImage originalImage = ImageIO.read(file);
                int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

                BufferedImage resizeImageJpg = resizeImage(originalImage, type);
                ImageIO.write(resizeImageJpg, "jpg", file);
                InputStream is = new FileInputStream(file);
                long length = file.length();

                // Create the byte array to hold the data
                byte[] bytes = new byte[(int) length];

                // Read in the bytes
                int offset = 0;
                int numRead = 0;
                while (offset < bytes.length
                        && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                    offset += numRead;
                }

                // Ensure all the bytes have been read in
                if (offset < bytes.length) {
                    throw new IOException("Could not completely read file " + file.getName());
                }

                // Close the input stream and return bytes
                is.close();
                K100Store store = new K100Store();
                String s = "abc";
                //bytes  -- nulll -- s
                System.out.println("-------------00000000-----------------------" + bytes + "--------" + bytes.length);
                if (pk100 != 0) {
                    int result = store.updateColumnLogo(pk100, bytes, getLogin().getUserName());
                    System.out.println("==++++++++++++++++++++++ length byte " + bytes.length + "--"
                            + "result" + result + "PK100" + pk100);
                    try {
                        if (result > 0) {
                            showGlobalReport(true, getResourceString("datasavesuccessful"));
                        } else {
                            showGlobalReport(true, getResourceString("datasuccessfail"));
                        }
                    } catch (Exception ex) {
                        System.out.println("ERROR:COULD NOT ATTACH FILE:CODE:" + ex.getMessage());
                    }
                } else if (pn100 != 0) {
                    EmployeeStore storeN100 = new EmployeeStore();
                    int result = storeN100.updateColumnLogoN100(pn100, bytes, getLogin().getUserName());
                    System.out.println("==++++++++++++++++++++++ length byte " + bytes.length + "--"
                            + "result" + result + "PK100" + pn100);
                    try {
                        if (result > 0) {
                            showGlobalReport(true, getResourceString("datasavesuccessful"));
                        } else {
                            showGlobalReport(true, getResourceString("datasuccessfail"));
                        }
                    } catch (Exception ex) {
                        System.out.println("ERROR:COULD NOT ATTACH FILE:CODE:" + ex.getMessage());
                    }
                }





            } catch (Exception ex) {
                System.out.println("ERROR:COULD NOT ATTACH FILE:CODE:" + ex.getMessage());
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
        }
    }

    /**
     * @return the pk100
     */
    public int getPk100() {
        return pk100;
    }

    /**
     * @param pk100 the pk100 to set
     */
    public void setPk100(int pk100) {
        this.pk100 = pk100;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the reload
     */
    public String getReload() {
        if (pagereload.equals("logo")) {
            reload = "cuscompany.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        } else if (pagereload.equals("logo1")) {
            reload = "supplier.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        } else if (pagereload.equals("logo2")) {
            reload = "customer.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        } else if (pagereload.equals("logo3")) {
            reload = "employee.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        } else if (pagereload.equals("logo4")) {
            reload = "myprofile.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        } else if (pagereload.equals("logo5")) {
            reload = "partner.jsp?"+Util.convertMD5(getRequest().getRequestURI());
        }
        return reload;
    }

    /**
     * @param reload the reload to set
     */
    public void setReload(String reload) {
        this.reload = reload;
    }

    /**
     * @return the pagereload
     */
    public String getPagereload() {
        return pagereload;
    }

    /**
     * @param pagereload the pagereload to set
     */
    public void setPagereload(String pagereload) {
        this.pagereload = pagereload;
    }

    /**
     * @return the pn100
     */
    public int getPn100() {
        return pn100;
    }

    /**
     * @param pn100 the pn100 to set
     */
    public void setPn100(int pn100) {
        this.pn100 = pn100;
    }

    /**
     * @return the beginContructor
     */
    public int getBeginContructor() {
        return beginContructor;
    }

    /**
     * @param beginContructor the beginContructor to set
     */
    public void setBeginContructor(int beginContructor) {
        this.beginContructor = beginContructor;
    }
}
